package com.sc.nft.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.dao.DaoAttentionDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.CollectionAttention;
import com.sc.nft.entity.DaoAttention;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.CollectionAttentionOrCancelDTO;
import com.sc.nft.entity.dto.DaoAttentionListVO;
import com.sc.nft.entity.dto.DaoAttentionOrCancelDTO;
import com.sc.nft.entity.dto.DaoWhetherAttentionDTO;
import com.sc.nft.entity.vo.DaoWhetherAttentionVO;
import com.sc.nft.entity.vo.WhetherAttentionVO;
import com.sc.nft.service.DaoAttentionService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * dao关注(DaoAttention)表服务实现类
 *
 * @author 王伟成
 * @since 2024-03-06 17:05:20
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DaoAttentionServiceImpl implements DaoAttentionService {

    private final DaoAttentionDao daoAttentionDao;
    @Override
    public void attentionOrCancel(UserInfo user, DaoAttentionOrCancelDTO request) {
        DaoAttention daoAttention = daoAttentionDao.getUserIdAndCollectionId(user.getId(), request.getDaoId());
        // 关注
        if (request.getAttention()) {
            attention(user, request, daoAttention);
        }
        // 取消关注
        if (!request.getAttention()) {
            cancelAttention(daoAttention);
        }
    }
    @Override
    public DaoWhetherAttentionVO whetherAttention(UserInfo user, DaoWhetherAttentionDTO request) {
        DaoWhetherAttentionVO whetherAttentionVO=new DaoWhetherAttentionVO();
        DaoAttention collectionAttention = daoAttentionDao.getUserIdAndCollectionId(user.getId(), request.getDaoId());
        if (BeanUtil.isNotEmpty(collectionAttention)) {
            whetherAttentionVO.setAttention(collectionAttention.getStatus());
        }else {
            whetherAttentionVO.setAttention(Boolean.FALSE);
        }
        return whetherAttentionVO;
    }
    @Override
    public IPage<DaoAttentionListVO> attentionList(UserInfo user, PageRequest pageRequest) {
        return daoAttentionDao.attentionList(user.getId(),pageRequest);
    }

    private void attention(UserInfo user, DaoAttentionOrCancelDTO request, DaoAttention daoAttention) {
        // 用户是否已关注
        if (BeanUtil.isEmpty(daoAttention)) {
            // 没有关注,新增一条.
            attentionSave(user, request);
        }
        // 有数据,但是不一定是关注
        if (BeanUtil.isNotEmpty(daoAttention)) {
            // 未关注,把数据修改为关注
            if (!daoAttention.getStatus()) {
                daoAttention.setStatus(Boolean.TRUE);
                daoAttention.updateById();
            }
        }
    }

    private void cancelAttention(DaoAttention daoAttention) {
        if (BeanUtil.isNotEmpty(daoAttention)) {
            daoAttention.setStatus(Boolean.FALSE);
            daoAttention.updateById();
        }
    }

    private void attentionSave(UserInfo user, DaoAttentionOrCancelDTO request) {
        DaoAttention save = new DaoAttention();
        save.setDaoId(request.getDaoId());
        save.setUserId(user.getId());
        save.setStatus(Boolean.TRUE);
        save.insert();
    }
}
